//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;

body;

beginstate INIT_STATE;
	set_name(ME,"Defender Turret");
	set_boss_level(ME,1);
	set_strategy(ME,10);
	set_attack_bonus(ME,20);
	set_new_abil(ME,20);
	
	if (gf(67,6) >= 30)
		set_summon_level(ME,1);
	break;

beginstate DEAD_STATE;
	inc_flag(67,6,1);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
			if (get_attitude(ME) == 4)
				set_attitude(ME,10);
			set_flag(get_memory_cell(0),get_memory_cell(1),0);
			}

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10) {
				set_attitude(ME,4);
				end();
				}
			}
			else if (get_flag(get_memory_cell(0),get_memory_cell(1)) == 0) {
				if (get_attitude(ME) == 4)
					set_attitude(ME,10);
				}
		}
		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_memory_cell(0) != 0) || (get_memory_cell(1) != 0)) {
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) > 0) {
			if (get_attitude(ME) >= 10)
				set_attitude(ME,4);
			set_foe_target(ME,-1);
			set_state(START_STATE);
			}
		}
	if (target_ok() == FALSE)
		set_state(START_STATE);
		
	// haw haw haw pwnt
	if ((dist_to_char(pc_num()) <= 8) && (can_see_char(pc_num())))
		set_foe_target(ME,pc_num());
		
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}
		
	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;